package com.gutsyzhan.activitifly;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;

@SpringBootTest
public class Part3_ProcessInstance {
    @Resource
    private RuntimeService runtimeService;

    /**
     * 初始化流程实例
     */
    @Test
    public void initProcessInstance() {
        //1、实际上在初始化流程之前，我们会先获取页面表单填报的内容，如请假时间、事由等，这些数据被封装到字符串类型的formData中
        //2、将formData数据写入业务表，并返回业务表的主键ID，这里的主键id就是我们需要的businessKey
        //3、之所以这样设计，是因为它可以将业务数据与Activiti7的流程数据进行绑定
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess_Part1", "bKey001");
        System.out.println("流程实例id："+ processInstance.getProcessInstanceId());
    }


    /**
     * 获取流程实例列表
     */
    @Test
    public void getProcessInstances() {
        List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery().list();
        for (ProcessInstance processInstance : processInstances) {
            System.out.println("processInstanceId:" + processInstance.getProcessInstanceId());
            System.out.println("processDefinitionId:" + processInstance.getProcessDefinitionId());
            System.out.println("isEnded:" + processInstance.isEnded());
            System.out.println("isSuspended:" + processInstance.isSuspended());
            System.out.println();
        }
    }

    /**
     * 暂停与激活流程实例
     */
    @Test
    public void activeProcessInstance() {
        //暂停流程实例
        runtimeService.suspendProcessInstanceById("ff6c8f31-90e1-11ee-8baf-f88c2116e1b9");
        //激活流程实例
        runtimeService.activateProcessInstanceById("ff6c8f31-90e1-11ee-8baf-f88c2116e1b9");
    }

    /**
     * 删除流程实例
     */
    @Test
    public void deleteProcessInstance() {
        runtimeService.deleteProcessInstance("ff6c8f31-90e1-11ee-8baf-f88c2116e1b9", "测试使用");
    }
}
